<?php namespace Common\Model;
	
	use Hdphp\Model\Model;
	
	class Gorder extends  Model{
		protected $table='gorder';
		
		
		public function store(){
//		$data=$_POST;
		
//		var_dump($data);die;
		$orderNumber=Cart::getOrderId();
//			收货人姓名
			$consignee=Q('post.consignee');
			
			$address=Q('post.address');
//			手机号
			$phone=Q('post.phone');
//			备注
			$remark=Q('post.odremark');
//			$total=
			$postcode=Q('post.postcode');
//			获取当前购物车已经选中的商品键名
			$glid=$_SESSION['cartglid'];
//			将键名数据传入方法中获取对应的商品数据
			$cartData=$this->getCartGoods($glid);

//		写入订单表，
			$data=array(
				'onumber'=>$orderNumber,
				'consignee'=>$consignee,
				'address'=>$address,
				'phone'=>$phone,
				'postcode'=>$postcode,
				'alltotal'=>$cartData['total_all'],
				'ordertime'=>time(),
				'odremark'=>$remark,
				'status'=>'未付款',
				'user_uid'=>$_SESSION['id'],
			);		
//		var_dump($this);die;
		$oid=$this->add($data);
			
//		var_dump($oid);die;	
		$orderlistModel=new \Common\Model\Gorderlist;
		
		foreach($cartData['goods'] as $k=>$v){
			$combine=$v['options']['combine'];
			$glid=Db::table('goods_list')->where('combine',$combine)->where('goods_gid',$v['id'])->pluck('glid');
			
			$glnumber=Db::table('goods_list')->where('combine',$combine)->where('goods_gid',$v['id'])->pluck('number');			
		
			$oderlistData=array(
				'num'=>$v['num'],
				'total'=>$v['total'],
				'remark'=>$remark,
				'goods_gid'=>$v['id'],
				'order_oid'=>$oid,
				'glnumber'=>$glnumber,
				'glid'=>$glid
			);
//		var_dump($orderlistModel);die;
			
		}		
		$orderlistModel->add($oderlistData);	
		return $oid;
	}	



	private function getCartGoods($glid){
		//		注意这里只需要获取选中的商品的数据信息
		$data=Cart::getGoods(); 
//		p($glid);
//		p($data);die;
		
		$comData=array();
		$totalData=array();
		foreach($data as $k=>$v){
//			当，当前商品的键名存在于所有选择中的商品id中
			if(in_array($k,$glid)){
				$comData['goods'][]=$v;
//				压入小计价格
				$totalData[]=$v['total'];
				
			}
		}
//		p($totalData);
//		求和array_sum
//		压入商品数组中
		$comData['total_all']=array_sum($totalData);
//		p($comData);die;
		return $comData;
	}












}	
	

	
	?>